import { MigrationInterface, QueryRunner } from 'typeorm';

export class InitDatabase1710000000000 implements MigrationInterface {
  name = 'InitDatabase1710000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建 permissions 表
    await queryRunner.query(`
      CREATE TABLE \`permissions\` (
        \`id\` int NOT NULL AUTO_INCREMENT,
        \`code\` varchar(20) NOT NULL,
        \`name\` varchar(50) NOT NULL,
        \`description\` varchar(100) NOT NULL,
        PRIMARY KEY (\`id\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建 roles 表
    await queryRunner.query(`
      CREATE TABLE \`roles\` (
        \`id\` int NOT NULL AUTO_INCREMENT,
        \`name\` varchar(20) NOT NULL,
        \`description\` varchar(100) NULL,
        PRIMARY KEY (\`id\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建 role_permissions 关联表
    await queryRunner.query(`
      CREATE TABLE \`role_permissions\` (
        \`role_id\` int NOT NULL,
        \`permission_id\` int NOT NULL,
        PRIMARY KEY (\`role_id\`, \`permission_id\`),
        KEY \`IDX_role_permissions_role_id\` (\`role_id\`),
        KEY \`IDX_role_permissions_permission_id\` (\`permission_id\`),
        CONSTRAINT \`FK_role_permissions_role_id\` FOREIGN KEY (\`role_id\`) REFERENCES \`roles\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE,
        CONSTRAINT \`FK_role_permissions_permission_id\` FOREIGN KEY (\`permission_id\`) REFERENCES \`permissions\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建 users 表
    await queryRunner.query(`
      CREATE TABLE \`users\` (
        \`id\` int NOT NULL AUTO_INCREMENT,
        \`username\` varchar(50) NOT NULL,
        \`phone_number\` varchar(20) NULL,
        \`password\` varchar(100) NOT NULL,
        \`email\` varchar(50) NULL,
        \`avatar\` varchar(100) NULL,
        \`is_frozen\` tinyint NOT NULL DEFAULT 0,
        \`is_admin\` tinyint NOT NULL DEFAULT 0,
        \`create_time\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
        \`update_time\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
        PRIMARY KEY (\`id\`),
        UNIQUE KEY \`IDX_users_username\` (\`username\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建 user_roles 关联表
    await queryRunner.query(`
      CREATE TABLE \`user_roles\` (
        \`user_id\` int NOT NULL,
        \`role_id\` int NOT NULL,
        PRIMARY KEY (\`user_id\`, \`role_id\`),
        KEY \`IDX_user_roles_user_id\` (\`user_id\`),
        KEY \`IDX_user_roles_role_id\` (\`role_id\`),
        CONSTRAINT \`FK_user_roles_user_id\` FOREIGN KEY (\`user_id\`) REFERENCES \`users\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE,
        CONSTRAINT \`FK_user_roles_role_id\` FOREIGN KEY (\`role_id\`) REFERENCES \`roles\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);

    // 创建 sourcemap 表
    await queryRunner.query(`
      CREATE TABLE \`sourcemap\` (
        \`id\` int NOT NULL AUTO_INCREMENT,
        \`filename\` varchar(255) NOT NULL,
        \`filepath\` varchar(255) NOT NULL,
        \`platform_type\` enum('standard', 'custom') NOT NULL DEFAULT 'standard',
        \`customer_name\` varchar(100) NULL,
        \`project_type\` enum('console', 'agent') NOT NULL,
        \`version\` varchar(50) NOT NULL,
        \`create_time\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
        \`update_time\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
        PRIMARY KEY (\`id\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 按照依赖关系的相反顺序删除表
    await queryRunner.query('DROP TABLE IF EXISTS \`sourcemap\`');
    await queryRunner.query('DROP TABLE IF EXISTS \`user_roles\`');
    await queryRunner.query('DROP TABLE IF EXISTS \`users\`');
    await queryRunner.query('DROP TABLE IF EXISTS \`role_permissions\`');
    await queryRunner.query('DROP TABLE IF EXISTS \`roles\`');
    await queryRunner.query('DROP TABLE IF EXISTS \`permissions\`');
  }
}
